<?php
class Application_Model_DbTable_Feed extends CF_DbTable_Table
{
	protected $_name = 'feeds';
	protected $_primary = 'id';
	protected $_columns = array(
								'link_id'=>null,
								'title'=>'',
								'description'=>'',
								'full_feed_link'=>''
								);


	/**
	 * fields:text , id , link_id
	 * concat fiels title and description
	 * then in got field title.description AS text
	 * if $link_id is set it will return
	 * text of that link otherwise
	 * all link feeds text
	 * @param $link_id

	 */

	public function getFeedsText($link_id = null){

		$select = $this->getAdapter()->select()
			 	  ->from('feeds',
			 	          array('id', 'link_id','text'=>'CONCAT(title," ", description)' ));

		if(!is_null($link_id)){
			$select->where('link_id=?',$link_id);
		}
		$resource = $this->getAdapter()->query($select);
		return $resource->fetchAll();
	}
	
	/**
	 * get all news which have a defenite tag
	 * from certain link(rss)
	 * in there their content 
	 * if tag_id is null return all feeds where there are some tag occurances
	 * if there is a link_id in arfuments it returns all feeds with
	 * such tag occurances and bound with certain link(rss)  
	 * @param $tag_id
	 * @param $link_id
	 */
	public function getFeedsByLinkIdWithCirtainTag($tag_id = null, $link_id = null)
	{
		
		$select = $this->getAdapter()->select()
						  			 ->from('feeds_tags')
						   		 	 ->joinLeft($this->_name,
						   				'feeds_tags.feed_id='.$this->_name.'.id',
						   				array('description','title','full_feed_link'));
		if(!is_null($tag_id)){
			$select->where('feeds_tags.tag_id=?',$tag_id);
		}
		if(!is_null($link_id)){
			$select->where('feeds_tags.link_id=?',$link_id);
		}
		$select->order('feeds_tags.number');
		$resources = $this->getAdapter()->query($select);
		
		return $resources->fetchAll();
	} 

}